package trunk.firstSec;

public class queryWrapper {
    private int   index;
    private byte[] pattern;
    private int[]  overlap;

    public queryWrapper(int in, byte[] p){
        index = in;  pattern = p;
        // input length of x
        int x = pattern.length;
        overlap = new int[x];
        overlap[0] = -1;                        // first one is -1 to prevent algo from backtracking further
        for (int i = 1; i < x - 1; i++)         // 0 + (1 -> i+1) cause loop operates 1 step further than i
        {
            byte currentChar = pattern[i];
            int offset = overlap[i];            // current no of char overlap w/ pattern header (if use com logic and array start at 0)
            while (pattern[offset] != currentChar && offset != 0)
                offset = overlap[offset];
            if (pattern[offset] == currentChar)
                overlap[i+1] = offset + 1;
            else
                overlap[i+1] = 0;
        } }

    public String toString()       { return new String(pattern);}

    public int      getIndex()     { return index;}
    public int[]    getOverlap()   { return overlap; }
    public byte[]   getPattern()   { return pattern; }


}
